package com.xmy.cultivate.controller;

import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.xmy.cultivate.entity.response.ArrivalRate;
import com.xmy.cultivate.service.IOrderDetailService;
import com.xmy.cultivate.util.Code;
import com.xmy.cultivate.util.PageResult;
import com.xmy.cultivate.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api/radeData")
public class RadeDataController extends BaseController{

    @Autowired
    IOrderDetailService iOrderDetailService;

    /**
     * 流失率
     * @return
     */
    @GetMapping("/getLosseRade")
    public Result getLosseRade(@RequestParam(value = "yearPart", required = false, defaultValue = "") String yearPart,
                               @RequestParam(value = "quarterNum", required = false, defaultValue = "") String quarterNum,
                               @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
                               @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
                               @RequestParam(value = "startDate", required = false, defaultValue = "") String startDate,
                               @RequestParam(value = "endDate", required = false, defaultValue = "") String endDate,
                               @RequestParam(value = "type", required = false, defaultValue = "") String type,
                               @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType,
                               @RequestParam(value = "yearClassId", required = false, defaultValue = "") String yearClassId
                               ){

        if (StringUtils.isBlank(schoolId)) {
            if (type.equals("2")) {
                //校区老师状态，只能看自己所在校区的数据
                schoolId = this.baseSchoolIdList.stream().map(Object::toString).collect(Collectors.joining(","));
            }
        }

        List<ArrivalRate> arrivalRateList =  iOrderDetailService.getLosseRade(yearPart,quarterNum,schoolId,subjectsId,startDate,endDate,type,lessonType,yearClassId);
        return new Result(Code.OK, new PageResult(arrivalRateList, (long) arrivalRateList.size(), 1L));

    }

    /**
     * 流失率详情
     * @return
     */
    @GetMapping("/getLosseRadeDetail")
    public Result getLosseRadeDetail(@RequestParam(value = "yearPart", required = false, defaultValue = "") String yearPart,
                               @RequestParam(value = "quarterNum", required = false, defaultValue = "") String quarterNum,
                               @RequestParam(value = "schoolId", defaultValue = "") String schoolId,
                               @RequestParam(value = "teacherId", defaultValue = "") String teacherId,
                               @RequestParam(value = "gradeId", defaultValue = "") String gradeId,
                               @RequestParam(value = "subjectsId", defaultValue = "") String subjectsId,
                               @RequestParam(value = "startDate", required = false, defaultValue = "") String startDate,
                               @RequestParam(value = "endDate", required = false, defaultValue = "") String endDate,
                               @RequestParam(value = "type", required = false, defaultValue = "") String type,
                               @RequestParam(value = "lessonType", required = false, defaultValue = "1") String lessonType,
                                     @RequestParam(value = "yearClassId", required = false, defaultValue = "") String yearClassId){
        List<ArrivalRate> arrivalRateList =  iOrderDetailService.getLosseRadeDetail(yearPart,quarterNum,schoolId,teacherId,gradeId,subjectsId,startDate,endDate,type,lessonType,yearClassId);
        return new Result(Code.OK, new PageResult(arrivalRateList, (long) arrivalRateList.size(), 1L));

    }
}
